package com.bort.system.controller;

import com.bort.system.BortApplication;
import com.bort.system.common.DataContainer;
import com.bort.system.info.LoginResult;
import com.bort.system.service.ClientService;
import com.bort.system.service.impl.ClientServiceImpl;
import com.bort.system.utils.ToastUtils;
import com.bort.system.view.MainView;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.awt.event.ActionListener;
import java.net.URL;
import java.util.ResourceBundle;

public class LoginController implements Initializable {

    private ClientService clientService;

    @FXML
    TextField userName;

    @FXML
    PasswordField password;

    @FXML
    Button loginBtn;

    @FXML
    public void onLoginBtnClick(ActionEvent event){
        String nameStr = userName.getText();
        String passwordStr = password.getText();
        LoginResult result = clientService.login(nameStr, passwordStr);
        if ("success".equals(result.getState())) {
            userName.setText(null);
            password.setText(null);
            DataContainer.loginUser = result.getData();
            Stage loginApp = BortApplication.getStage();
            loginApp.close();
            BortApplication.showView(MainView.class);
        } else {
            ToastUtils.toast(result.getMsg());
        }
    }

    public void initialize(URL location, ResourceBundle resources) {
        BortApplication.getStage().setResizable(false);
        userName.setPromptText("请输入手机号");
        password.setPromptText("请输入密码");
        userName.setText(null);
        password.setText(null);
        clientService = ClientServiceImpl.getInstance();
    }
}
